<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="form" :rules="rules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">
          <img src="https://likede2-admin.itheima.net/img/logo.595745bd.png" alt="">
        </h3>
      </div>
      <el-form-item prop="loginName">
        <span class="svg-container el-icon-mobile" />
        <el-input v-model="form.loginName" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container el-icon-lock" />
        <el-input ref="pwdinp" v-model="form.password" placeholder="请输入密码" :type="inputType" />
        <span class="svg-container" @click="changeType">
          <svg-icon :icon-class="inputType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <span class="svg-container el-icon-key" />
        <el-input v-model="form.code" placeholder="请输入验证码" />
      </el-form-item>
      <span class="code" @click="changeImg"><img :src="this.$store.state.user.imgSrc" alt=""></span>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: admin</span>
      </div>

    </el-form>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    return {
      num: Math.random().toString(36).substring(2),
      loading: false,
      form: {
        loginName: 'demo',
        password: '888itcast.CN764%...',
        code: '',
        clientToken: '123',
        loginType: 0
      },
      inputType: 'password',
      rules: {
        loginName: [
          { required: true, message: 'Please enter', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'Please enter', trigger: 'blur' }
        ],
        code: [
          { required: true, message: 'Please enter', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.$store.dispatch('user/codeAction', this.form.clientToken)
    this.form.clientToken = this.num
  },
  methods: {
    changeType() {
      this.inputType === 'password' ? this.inputType = 'text' : this.inputType = 'password'
      this.$nextTick(() => {
        this.$refs.pwdinp.focus()
      })
    },
    async handleLogin() {
      try {
        this.loading = true
        await this.$refs.loginForm.validate()
        console.log('数据通过')
        await this.$store.dispatch('user/loginAction', this.form)
        this.loading = false
        this.$router.push('/')
      } catch (e) {
        console.log(e)
        this.loading = false
      }
    },
    changeImg() {
      // this.num = Math.random().toString(36).substring(2)
      this.$store.dispatch('user/codeAction', this.num)
    }
  }
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#454545;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .code {
    position: absolute;
    top: 290px;
    right: 36px;
    img {
      height: 47px;
    }
  }
  .el-input {
    position: relative;
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $light_gray;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0,0.1);
    background: rgba(255, 255, 255,0.7);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background-image: url('https://likede2-admin.itheima.net/img/background.be4fae7d.png');
  background-position: center;
  overflow: hidden;

  .login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 520px;
    max-width: 100%;
    padding: 0px 35px 0;
    // margin: 280px auto;
    // overflow: hidden;
    background-color: #fff;
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      img {
        width: 96px;
        height: 96px;
        transform: translateY(-50%);
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
